package cc.lj.internal.coupons.application;

import cc.lj.internal.api.coupons.api.ticket.DomainTicketFeignService;
import cc.lj.internal.api.coupons.dto.DomainResponseDTO;
import cc.lj.internal.api.coupons.dto.command.SendTicketsAPICommand;
import cc.lj.internal.api.merchant.api.PropertyRelationFeignService;
import cc.lj.internal.api.merchant.api.PropertyResourcesFeignService;
import cc.lj.internal.api.merchant.dto.PropertyRelatonApiDTO;
import cc.lj.internal.api.merchant.dto.PropertyResourcesApiDTO;
import cc.lj.internal.api.merchant.query.PropertyRelationApiQuery;
import cc.lj.internal.api.ordershare.api.OrderOnlineFeignService;
import cc.lj.internal.api.ordershare.dto.OrderOnlineAPIDTO;
import cc.lj.internal.core.exception.ServiceException;
import cc.lj.internal.core.http.HttpResult;
import cc.lj.internal.coupons.infrastructure.excepion.CoupousExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 应用层通用调用外部服务类
 * @author liangxifeng
 * @date 2021-12-29
 */
@Slf4j
@Service("feignAppService")
public class FeignAppService {
    //商户模块,物业资源展位表 feign api
    @Resource(type = PropertyResourcesFeignService.class)
    private PropertyResourcesFeignService propertyResourcesFeignService;

    //商户模块,物业资源分类表 feign api
    @Resource(type = PropertyRelationFeignService.class)
    private PropertyRelationFeignService propertyRelationFeignService;

    //公有库模块，订单表order_online feign api
    @Resource(type = OrderOnlineFeignService.class)
    private OrderOnlineFeignService orderOnlineFeignService;

    //8080 领域层 domain_ticket 项目 api
    @Resource(type = DomainTicketFeignService.class)
    private DomainTicketFeignService domainTicketFeignService;

    /**
     * 某个区域下的在租展位以及该展位的日租金接口List
     * @param relationId 物业资源分表property_relation主键
     * @return 在租展位以及该展位的日租金List
     */
    public List<PropertyResourcesApiDTO> getRentingResourceListByArea(Integer relationId){
        HttpResult<List<PropertyResourcesApiDTO>> listHttpResult = propertyResourcesFeignService.getRentingResourceListByArea(relationId);
        if(listHttpResult == null || listHttpResult.getCode() != 1) {
            log.info("去商户服务通过物业资源分类表property_relation主键查询该区域下在租展位时出现错误，错误返回:"+listHttpResult);
            return new ArrayList<>();
        }
        return listHttpResult.getData();
    }
    /**
     * 查询商户展位区域列表
     */
    public List<PropertyRelatonApiDTO> showAreaList(PropertyRelationApiQuery propertyRelationApiQuery){
        HttpResult<List<PropertyRelatonApiDTO>> listHttpResult = propertyRelationFeignService.showAreaList(propertyRelationApiQuery);
        log.info("去商户服务查询展位区域列表,入参："+propertyRelationApiQuery);
        if(listHttpResult == null || listHttpResult.getCode() != 1) {
            log.info("去商户服务查询展位区域列表失败，错误返回:"+listHttpResult);
            return new ArrayList<>();
        }
        return listHttpResult.getData();
    }

    /**
     * 通过合同号去 ordershare 模块查询订单数据
     * @param pactNum
     * @return 订单数据
     */
    public OrderOnlineAPIDTO getOrderByPactNum(String pactNum){
        HttpResult<OrderOnlineAPIDTO> result = orderOnlineFeignService.getByPactNum(pactNum);
        if(result.getCode() != 1) {
            log.info("去公有库模块ordershare通过合同号查询订单数据,错误返回："+result);
        }
        log.info("去公有库模块ordershare通过合同号查询订单数据，查询订单数据=："+result.getData());
        return result.getData();
    }

    /**
     * 调用领域层 8080 端口 domain_ticket 项目 发券接口，发送电子券
     * @param sendTicketsAPICommand
     * @return List<电子券使用码>
     */
    public List<String> sendTicket(SendTicketsAPICommand sendTicketsAPICommand) {
        DomainResponseDTO<Map<String,Object>> sendRes = domainTicketFeignService.sendTicket(sendTicketsAPICommand);
        //log.info("去domain_ticket模块调用发券接口,返回："+sendRes);
        if(sendRes.getCode() != 1)
        {
            String error = "Feign调用domain_ticket发券接口，发券失败:"+ sendTicketsAPICommand +"失败返回:"+sendRes;
            log.error(error);
            throw new ServiceException(CoupousExceptionEnum.FEIGN_DOMAIN_TICKET_SENDTICKET_ERROR);
        }
        //存储券使用码 List
        List<String> useCodeList = new ArrayList<>();
        //如果成功返回打印info日志，打出发放的券使用码
        if(sendRes.getData().get("bathTickets") != null) {
            List batchList = (List) sendRes.getData().get("bathTickets");
            //券批次列表 遍历
            for (int i = 0; i < batchList.size(); i++) {
                List tikcetList = (List) ((Map) batchList.get(i)).get("tickets");
                //券主表ticket_main 表数据遍历
                for (int j = 0; j < tikcetList.size(); j++) {
                    //电子券数据
                    Map ticket = (Map) tikcetList.get(j);
                    useCodeList.add(ticket.get("ticketUseCode").toString());
                }
            }
        }else {
            log.error("Feign调用domain_ticket发券接口成功，但是获取券批次列表数据失败，调用返回值="+sendRes);
        }
        return useCodeList;
    }
}
